/*
 * @(#)RespondingRetrieveTransformer.java
 * Date 2013-10-21
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.xca.transformer;

import javax.annotation.PostConstruct;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import ihe.iti.xds_b._2007.RetrieveDocumentSetRequestType;

import ihe.iti.xds_b._2007.RetrieveDocumentSetRequestType.DocumentRequest;

import org.apache.commons.lang3.StringUtils;

import us.hornerscorners.lamppost.exception.ServiceActionError;
import us.hornerscorners.lamppost.exception.ServiceActionException;
import us.hornerscorners.lamppost.exception.SeverityType;

import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;

import us.hornerscorners.lamppost.registry.constant.ErrorCode;

import us.hornerscorners.lamppost.repository.client.RepositoryClient;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;

import us.hornerscorners.lamppost.xca.model.RepositoryClients;
import us.hornerscorners.lamppost.xca.model.RetrieveRequest;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-10-21
 * @author         Jim Horner
 */
@ApplicationScoped
public class RespondingRetrieveTransformer
        extends AbstractTransformer<RetrieveDocumentSetRequestType, RetrieveRequest>
        implements IRetrieveTransformer {

    /** Field description */
    @Inject
    private IHEConfig config;

    /** Field description */
    private String homeCommunityId;

    /** Field description */
    @Inject
    private RepositoryClients repositoryClients;

    /**
     * Constructs ...
     *
     */
    public RespondingRetrieveTransformer() {
        super();
    }

    /**
     * Method description
     *
     */
    @PostConstruct
    public void postConstruct() {

        this.homeCommunityId = this.config.getHomeCommunityId();
    }

    /**
     * Method description
     *
     *
     * @param body
     *
     * @return
     *
     * @throws ServiceActionException
     */
    @Override
    public RetrieveRequest transform(RetrieveDocumentSetRequestType body)
            throws ServiceActionException {

        RetrieveRequest result = new RetrieveRequest(this.repositoryClients);

        for (DocumentRequest request : body.getDocumentRequest()) {

            boolean errored = false;
            String reqHome = request.getHomeCommunityId();
            String reqDocId = request.getDocumentUniqueId();
            String reqRepId = request.getRepositoryUniqueId();

            if (StringUtils.isBlank(reqHome)) {

                String msg =
                    String.format("Missing Home Community Id for Document [%s].",
                                  reqDocId);

                result.addError(
                    new ServiceActionError(
                        ErrorCode.XDSMissingHomeCommunityId.getCode(), msg,
                        SeverityType.Error));

                errored = true;

            } else if (this.homeCommunityId.equals(reqHome) == false) {

                String msg =
                    String.format("Unknown Home Community Id [%s] for Document [%s].",
                                  reqHome, reqDocId);

                result.addError(
                    new ServiceActionError(
                        ErrorCode.XDSUnknownCommunity.getCode(), msg,
                        SeverityType.Error));

                errored = true;
            }

            RepositoryClient client = this.repositoryClients.find(reqRepId);

            if (client == null) {

                String msg =
                    String.format("Unknown Repository Id [%s] for Document [%s].",
                                  reqRepId, reqDocId);

                result.addError(
                    new ServiceActionError(
                        ErrorCode.XDSUnknownRepositoryId.getCode(), msg,
                        SeverityType.Error));

                errored = true;
            }

            if (errored == false) {

                result.addRequest(reqRepId, request);
            }
        }

        return result;
    }
}
